package cc.vv.btpartyjob.jnimodule.fragment

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.jnimodule.R
import cc.vv.btpartyjob.jnimodule.adapter.DemonstrationAdapter
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.delegate.DemonstrationFragmentDelegate
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.moudel.bean.NewsBean
import cc.vv.btpartyjob.jnimodule.moudel.resp.NewsResponseObj
import cc.vv.btpartyjob.jnimodule.server.JniDataServer
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 示范典型列表功能的fragment
 * Created by Chen.qi on 2018/10/29.
 */
class DemonstrationFragment : BaseFragmentMVP<DemonstrationFragmentDelegate>(), SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener  {
    /**
     * 当前页数
     */
    private var pageNo: Int = 1

    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 数据适配器
     */
    private var mAdapter: DemonstrationAdapter? = null

    /**
     * 类型  0 农村党组织 1 社区党组织 2 学校党组织
     * 3 机关党组织 4 非公党组织 5 社会组织党组织 6 国家企事业单位党组织
     */
    private var type: String = "0"

//    /**
//     * item点击事件
//     */
//    private var onItemClick: BaseQuickAdapter.OnItemClickListener? = null
//
//    /**
//     * 设置点击事件
//     */
//    fun setOnItemClick(onItemClick: BaseQuickAdapter.OnItemClickListener) {
//        this.onItemClick = onItemClick
//    }

    /**
     * 设置type
     */
    fun setType(type: String) {
        this.type = type
    }

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<NewsBean>? = null

    override fun initData() {
        mDataList = ArrayList()
        mDataList!!.clear()
        if (mAdapter == null) {
            mAdapter = DemonstrationAdapter(R.layout.item_data_list)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
    }

    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
    }

    override fun getDelegateClass(): Class<DemonstrationFragmentDelegate> {
        return DemonstrationFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    private var whetherRefresh = false

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            pageNo = 1
            JniDataServer.getDemonstrationList(pageNo, pageSize, type, true)
            whetherRefresh = true
        }
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        JniDataServer.getDemonstrationList(pageNo, pageSize, type, false)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        JniDataServer.getDemonstrationList(pageNo, pageSize, type, false)
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            viewDelegate?.stopRefreshing()
            when (obj) {
                is NewsResponseObj -> {
                    if (null == obj.data) {
                        return
                    }
                    if (1 == pageNo) {
                        mDataList?.clear()
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                        mAdapter?.disableLoadMoreIfNotFullPage()
                    } else {
                        if (mAdapter?.data?.size!! >= obj.data.total) {
                            //数据全部加载完毕
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        } else {
                            //成功获取更多数据
                            if (null != obj.data.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            mAdapter?.setNewData(mDataList)
                            //加载数据
                            mAdapter?.disableLoadMoreIfNotFullPage()
                            mAdapter?.loadMoreComplete()

                        }
                    }

                }
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
//        JniStartActivity.startIntentWebActivity(this, "详情", "http://www.baidu.com")
        JniStartActivity.startIntentWebActivity(context, H5UrlEnum.示范典型详情, mDataList?.get(position)?.id)
    }

}